<?php


namespace app\utils;


class DateTime
{
    public static function toTime($time){
        if ($time != null)$time = strtotime($time);
        return $time;
    }

    public static function toPreciseDay($time){
        if ($time != null)$time = strtotime(date('Y-m-d', strtotime($time)));
        return $time;
    }

    public static function toPreciseTime($time, $format = 'Hi'){
        if ($time != null)$time = date($format, strtotime($time));
        return $time;
    }

    public static function timeToDate($time , $format = 'Y-m-d H:i:s'){
        if ($time != null)$time = date($format , $time);
        return $time;
    }

    public static function getDay($start_time , $end_time){
        return ceil(abs(self::toTime($end_time) - self::toTime($start_time)) / (60*60*24));
    }


   public static function calculateAge($birthday, $day) {
        // 将生日转换为时间戳
        $birthTimestamp = strtotime($birthday);

        // 获取当前时间戳
        $currentTimestamp = strtotime($day);

        // 计算年龄
        $age = date('Y', $currentTimestamp) - date('Y', $birthTimestamp);

        // 如果当前日期小于生日对应的日期，则减去一岁
        if (date('md', $currentTimestamp) < date('md', $birthTimestamp)) {
            $age--;
        }

        return $age;
    }
    public static function getUnixTimestamp()
    {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);

    }
    public static function d2m($day)
    {
        return date('dMy', strtotime($day));
    }

}